package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IMerchantService;
import com.example.mybatisplusdemo.model.domain.Merchant;


/**
 * 前端控制器
 *
 * @author lxp
 * @since 2025-07-04
 * @version v1.0
 */
@RestController
@RequestMapping("/api/merchant")
public class MerchantController {

    private final Logger logger = LoggerFactory.getLogger(MerchantController.class);

    @Autowired
    private IMerchantService merchantService;

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        Merchant merchant = merchantService.getById(id);
        return JsonResponse.success(merchant);
    }

    // 登录方法
    //根据用户名登录
    @PostMapping("loginByUsername")
    public JsonResponse loginByUsername(@RequestBody Merchant merchant) {
        Merchant merchant_login = merchantService.loginByUsername(merchant);
        return JsonResponse.success(merchant_login);
    }
    //根据手机号登录
    @PostMapping("loginByPhone")
    public JsonResponse loginByPhone(@RequestBody Merchant merchant) {
        Merchant merchant_login = merchantService.loginByPhone(merchant);
        return JsonResponse.success(merchant_login);
    }
    //根据邮箱登录
    @PostMapping("loginByEmail")
    public JsonResponse loginByEmail(@RequestBody Merchant merchant) {
        Merchant merchant_login = merchantService.loginByEmail(merchant);
        return JsonResponse.success(merchant_login);
    }
    //根据微信登录
    @PostMapping("loginByWechat")
    public JsonResponse loginByWechat(@RequestBody Merchant merchant) {
        Merchant merchant_login = merchantService.loginByWechat(merchant);
        return JsonResponse.success(merchant_login);
    }

    // 注册方法 前端注册应该填写的信息包括：手机号/邮箱/微信，密码，头像
    //根据手机号注册
    @PostMapping("registerByPhone")
    public JsonResponse registerByPhone(@RequestBody Merchant merchant) {
        boolean isRegistered = merchantService.registerByPhone(merchant);
        //如果手机号已存在，返回失败
        return JsonResponse.success(isRegistered);
    }
    //根据邮箱注册
    @PostMapping("registerByEmail")
    public JsonResponse registerByEmail(@RequestBody Merchant merchant) {
        boolean isRegistered = merchantService.registerByEmail(merchant);
        //如果邮箱已存在，返回失败
        return JsonResponse.success(isRegistered);
    }
    //根据微信注册
    @PostMapping("registerByWechat")
    public JsonResponse registerByWechat(@RequestBody Merchant merchant) {
        boolean isRegistered = merchantService.registerByWechat(merchant);
        //如果微信号已存在，返回失败
        return JsonResponse.success(isRegistered);
    }

    //获取登录的商家信息
    @GetMapping("getInfo")
    public JsonResponse getInfo() {
        Merchant currentMerchantInfo = SessionUtils.getCurrentMerchantInfo();
        return JsonResponse.success(currentMerchantInfo); //把当前商家信息送到前端
    }

    @GetMapping("deleteById")
    public JsonResponse deleteById(Long id) {
        boolean res = merchantService.removeById(id);
        return JsonResponse.success(res);
    }

    @GetMapping("listMerchants")
    public JsonResponse listMerchants(PageDTO pageDTO, Merchant merchant) {
        Page<Merchant> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = merchantService.listMerchants(page, merchant);
        return JsonResponse.success(page);
    }

    @GetMapping("freezeById")
    public JsonResponse freezeById(Long id) {
        boolean res = merchantService.freezeById(id);
        return JsonResponse.success(res);
    }

    // 条件查询商户列表
    @GetMapping("unFreezeById")
    public JsonResponse unFreezeById(Long id) {
        boolean res = merchantService.unFreezeById(id);
        return JsonResponse.success(res);
    }

    //更新商家信息
    @PostMapping("updateMerchant")
    public JsonResponse updateMerchant(@RequestBody Merchant merchant) {
        boolean isUpdated = merchantService.updateById(merchant);
        if (isUpdated) {
            SessionUtils.saveCurrentMerchantInfo(merchant); // 更新当前商家信息到会话
            return JsonResponse.success("商家信息更新成功");
        } else {
            return JsonResponse.success("商家信息更新失败");
        }
    }

    //商家注册方法
    @GetMapping("/merchantRegister")
    public JsonResponse register(Merchant registerData) {
        try {
            boolean result = merchantService.merchantRegister(registerData);
            return JsonResponse.success(result);
        } catch (Exception e) {
            return JsonResponse.failure("注册失败: " + e.getMessage());
        }
    }
}

